"use strict";

$(function () {
  var token = localStorage.getItem("token");
  var id = localStorage.getItem("id");
  var username = localStorage.getItem("username"); //二维码

  if (!token && !id) {
    //表示你还未登录
    $(".islogin").hide();
    $(".notlogin").show();
  } else {
    $(".islogin").show();
    $(".notlogin").hide();
    $(".islogin_name").text(username);
  }

  $(".islogin_exit").click(function () {
    localStorage.removeItem("token");
    localStorage.removeItem("id");
    localStorage.removeItem("username");
    location.reload();
  });

  if (token && id) {
    $.get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
      id: id
    }, function (res) {
      $(".ci-count").text(res.data.length);
    });
  }

  $("#qrcode").hover(function () {
    $(".qrcode-big").show().css({
      "z-index": 4
    });
  }, function () {
    $(".qrcode-big").hide();
  }); //导航栏下拉菜单

  $(".islogin").hover(function () {
    $(".islogin_dorp").show();
  }, function () {
    $(".islogin_dorp").hide();
  });
  $("#ttbar-myjd").hover(function () {
    $(".dropdown-layer").show().css("z-index", 4);
  }, function () {
    $(".dropdown-layer").hide();
  });
  $("#ttbar-myjd1").hover(function () {
    $(".dropdown-layer1").show().css("z-index", 4);
  }, function () {
    $(".dropdown-layer1").hide();
  }); //定位侧边栏

  $("#J-global-toolbar .jd-tab").hover(function () {
    $(this).addClass("jd-tab-hover");
  }, function () {
    $(this).hasClass("jd-tab-hover");
    $(this).removeClass("jd-tab-hover");
  });
  $(".chaoshi-muen_list").click(function () {
    $(".chaoshi-muen_list a").removeClass("chaoshi-muen_list_title_hover");
    $(this).find("a").addClass("chaoshi-muen_list_title_hover");
  }); //分类侧边栏

  $(".item").hover(function () {
    $(".item_body").show();
  }, function () {
    $(".item_body").hide();
  });
  $.get("http://jx.xuzhixiang.top/ap/api/productlist.php", {
    uid: 74364,
    pagesize: 3
  }, function (res) {
    //console.log(res);
    var str = "";
    res.data.forEach(function (item) {
      str += "\n        <li class=\"J_goods_item cdb_item \" data-id=".concat(item.pid, ">\n            <div class=\"cdb_bd\">\n              <a  class=\"cdb_pic\">\n                <img\n                  src=\"").concat(item.pimg, "\"\n                  alt=\"\"\n                />\n              </a>\n              <a class=\"cdb_name\">\n                ").concat(item.pdesc, "\n              </a>\n            </div>\n            <div class=\"cdb_ft clearfix\">\n              <div class=\"cdb_ft_l fl\">\n                <p class=\"cdb_ft_l_price\">\n                  \u91CF\u8D29\u7248: &nbsp;<span>\uFFE5 ").concat(item.pprice, "</span>\n                </p>\n              </div>\n              <a class=\"cdb_add_cart fr\" data-id=\"").concat(item.pid, "\">\n                <i class=\"cdb_add_icon \"></i>\n                \u52A0\u5165\u8D2D\u7269\u8F66\n              </a>\n            </div>\n          </li>");
    });
    $(".chaoshi_discount_big").html(str);
    $(".cdb_item").click(function (e) {
      e.stopPropagation();
      localStorage.setItem("pid", $(this).attr("data-id"));
      location.href = "./datails.html";
    });
    $(".cdb_add_cart").click(function (e) {
      e.stopPropagation();

      if (!token && !id) {
        alert("请先登录");
        return;
      } else {
        $.get("http://jx.xuzhixiang.top/ap/api/add-product.php", {
          pnum: 1,
          pid: $(this).attr("data-id"),
          uid: id
        }, function (data) {
          alert("加入购物车成功");

          if (token && id) {
            $.get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
              id: id
            }, function (res) {
              $(".ci-count").text(res.data.length);
            });
          }
        });
      }
    });
  });
  $.get("http://jx.xuzhixiang.top/ap/api/productlist.php", {
    uid: 74364,
    pagesize: 5
  }, function (res) {
    var str1 = "";
    res.data.forEach(function (item) {
      str1 += "<li class=\"chaoshi_discount_list\" data-id=\"".concat(item.pid, "\">\n                <img src=\"").concat(item.pimg, "\" alt=\"\">\n                <a class=\"cs_di_title\">\n                ").concat(item.pdesc, "\n                </a>\n                <span class=\"cs_di_price\">\n                  \uFFE5 ").concat(item.pprice, "\n                </span>\n                <a class=\"cs_di_add\" data-id=\"").concat(item.pid, "\">\n                  <i class=\"cs_di_add_icon\"></i>\n                </a>\n              </li>");
    });
    $(".chaoshi_discount_s_item").html(str1);
    $(".chaoshi_discount_list").click(function (e) {
      e.stopPropagation();
      localStorage.setItem("pid", $(this).attr("data-id"));
      location.href = "./datails.html";
    });
    $(".cs_di_add").click(function (e) {
      e.stopPropagation();

      if (!token && !id) {
        alert("请先登录");
        return;
      } else {
        $.get("http://jx.xuzhixiang.top/ap/api/add-product.php", {
          pnum: 1,
          pid: $(this).attr("data-id"),
          uid: id
        }, function (data) {
          alert("加入购物车成功");

          if (token && id) {
            $.get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
              id: id
            }, function (res) {
              $(".ci-count").text(res.data.length);
            });
          }
        });
      }
    });
  });
  $("#tab-top").click(function () {
    $("body,html").stop().animate({
      scrollTop: 0
    }, 600);
  }); //热销商品遍历

  $.get("http://jx.xuzhixiang.top/ap/api/productlist.php", {
    uid: 74364,
    pagenum: 1,
    pagesize: 10
  }, function (res) {
    //console.log(res);
    var str2 = "";
    res.data.forEach(function (item) {
      str2 += "  <div class=\"swiper-slide\">\n            <div class=\"cs_buy_list\" data-id=\"".concat(item.pid, "\">\n              <img src=\"").concat(item.pimg, "\" alt=\"\">\n              <p class=\"\">").concat(item.pdesc, "</p>\n              <span class=\"\" style=\"display: none;\">\uFFE5").concat(item.pprice, "</span>\n              <em>\uFFE5").concat(item.pprice, "</em>\n            <button class=\"cs_buy_add\" data-id=\"").concat(item.pid, "\"\">\n              <i class=\"cs_buy_add_icon\"></i>\n              <strong>\u52A0\u5165\u8D2D\u7269\u8F66</strong>\n            </button>\n          </div>\n        </div>");
    });
    $("#swiper2").html(str2);
    $(".cs_buy_list").click(function (e) {
      e.stopPropagation();
      localStorage.setItem("pid", $(this).attr("data-id"));
      location.href = "./datails.html";
    });
    $(".cs_buy_add").click(function (e) {
      e.stopPropagation();

      if (!token && !id) {
        alert("请先登录");
        return;
      } else {
        $.get("http://jx.xuzhixiang.top/ap/api/add-product.php", {
          pnum: 1,
          pid: $(this).attr("data-id"),
          uid: id
        }, function (data) {
          alert("加入购物车成功");

          if (token && id) {
            $.get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
              id: id
            }, function (res) {
              $(".ci-count").text(res.data.length);
            });
          }
        });
      }
    });
    var swiper3 = new Swiper(".swiper3", {
      slidesPerView: 5,
      freeMode: true,
      loop: true,
      slidesPerGroup: 5,
      autoplay: {
        delay: 2000
      },
      navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev"
      }
    });

    swiper3.el.onmouseover = function () {
      swiper3.autoplay.stop();
    };

    swiper3.el.onmouseout = function () {
      swiper3.autoplay.start();
    };

    $(".cs_buy_list").hover(function () {
      $(this).find("p").css({
        display: "none"
      });
      $(this).find("em").css({
        display: "none"
      });
      $(this).find("span").css({
        display: "block"
      });
      $(this).find("button").stop().animate({
        bottom: 0
      }, 200);
    }, function () {
      $(this).find("p").css({
        display: "block"
      });
      $(this).find("em").css({
        display: "block"
      });
      $(this).find("span").css({
        display: "none"
      });
      $(this).find("button").stop().animate({
        bottom: "-61px"
      }, 200);
    });
  }); //奶制品数据遍历

  $.get("http://jx.xuzhixiang.top/ap/api/productlist.php", {
    uid: 74364,
    pagenum: 0,
    pagesize: 10
  }, function (res) {
    var str = "";
    res.data.forEach(function (item) {
      str += "\n        <li class=\"chaoshi_bd_r_item\" data-id=\"".concat(item.pid, "\">\n              <img src=\"").concat(item.pimg, "\" alt=\"\">\n              <p class=\"cs_bd_r_i_t\">").concat(item.pdesc, "</p>\n              <span class=\"cs_bd_r_i_p\">\uFFE5").concat(item.pprice, "</span>\n              <button class=\"cs_bd_add\" data-id=\"").concat(item.pid, "\">\n                <i class=\"cs_bd_add_icon\"></i>\n                <strong>\u52A0\u5165\u8D2D\u7269\u8F66</strong>\n              </button>\n            </li>");
    }); //console.log(str);

    $(".chaohi_bd_right").html(str);
    $(".chaoshi_bd_r_item").hover(function () {
      $(this).find("p").css({
        display: "none"
      });
      $(this).find("span").stop().animate({
        bottom: "70px"
      }, 200);
      $(this).find(".cs_bd_add").stop().animate({
        bottom: 0
      }, 200);
    }, function () {
      $(this).find("span").stop().animate({
        bottom: "20px"
      }, 50);
      $(this).find(".cs_bd_add").stop().animate({
        bottom: "-51px"
      }, 100);
      $(this).find("p").css({
        display: "block"
      });
    });
    $(".chaoshi_bd_r_item").click(function (e) {
      e.stopPropagation();
      localStorage.setItem("pid", $(this).attr("data-id"));
      location.href = "./datails.html";
    });
    $(".cs_bd_add").click(function (e) {
      e.stopPropagation();

      if (!token && !id) {
        alert("请先登录");
        return;
      } else {
        $.get("http://jx.xuzhixiang.top/ap/api/add-product.php", {
          pnum: 1,
          pid: $(this).attr("data-id"),
          uid: id
        }, function (data) {
          alert("加入购物车成功");
        });
      }
    });
  });
  var fg = true;
  $(window).scroll(function () {
    var st = $(this).scrollTop();

    if (st > 50) {
      $("#floorNav").fadeIn();
    } else {
      $("#floorNav").fadeOut();
    }

    if (fg) {
      $(".bb").each(function () {
        if (st >= $(this).offset().top - $(this).outerHeight() / 2) {
          var index = $(this).index() - 2; //console.log(index);

          $("#floorNav li").eq(index).addClass("hover").siblings().removeClass("hover");
        }
      });
    }
  });
  $("#floorNav li:not(:last)").click(function () {
    fg = false;
    $(this).addClass("hover").siblings().removeClass("hover");
    var index = $(this).index(); //console.log(index);

    $("body,html").animate({
      scrollTop: $(".bb").eq(index).offset().top
    }, 500, function () {
      fg = true;
    });
  });
  $("#floorNav li:last").click(function () {
    fg = false;
    $(this).siblings().removeClass("hover");
    $("body,html").animate({
      scrollTop: 0
    }, 500, function () {
      fg = true;
    });
  });
  $.get("http://jx.xuzhixiang.top/ap/api/all-province.php", function (res) {
    //console.log(res);
    var str = "";
    res.data.forEach(function (item) {
      //console.log(item);
      str += "<li class=\"city\"><a>".concat(item.name, "</a></li>");
    });
    $(".di_ite").html(str);
  });
  $(".dizhi").hover(function () {
    $(".diming").show().css({
      "z-index": 6
    });
  }, function () {
    $(".diming").hide().css({
      "z-index": 1
    });
  });
});